//
//  BookShelfViewController.m
//  FunRead
//
//  Created by  King on 15/5/22.
//  Copyright (c) 2015年 华东伟. All rights reserved.
//

#import "BookShelfViewController.h"
#import "SANetWorkingTask.h"
#import "CollectClass.h"
#import "BookDetailController.h"
@interface BookShelfViewController ()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, assign)TableViewEditType isEdit;
@property(nonatomic, retain)UITableView *table;
@property(nonatomic, retain)UIButton *button;
// 书架的书来自于本地, 存放到数组中
@property(nonatomic, retain)NSMutableArray *tableArray;

          
@end

@implementation BookShelfViewController

- (void)dealloc
{
    [_tableArray release];
    [_table release];
    [_button release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if ( self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.navigationController.navigationBar.translucent = NO;
    self.tabBarController.tabBar.translucent = NO;
    self.view.frame = CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height );
    self.navigationItem.title = @"书架";
    self.button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
    [self.button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.button setTitle:@"编辑" forState:UIControlStateNormal];
    [self.button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.button];
    
    [self createTable];
    
}

#warning 第一步:让tableView处于编辑状态
- (void)action:(id)sender
{
    // no滑动 yes 不滑
//    UIButton *button = (UIButton *)sender;
    if (self.isEdit == TableViewTypeIsEditing ) {
        [self.button setTitle:@"完成" forState:UIControlStateNormal];
        [self.table setEditing:YES animated:YES];
        self.isEdit = TableViewTypeNomal;
    }else if
        (self.isEdit ==  TableViewTypeNomal )
    {
        [self.table setEditing:NO animated:YES];
        [self.button setTitle:@"编辑" forState:UIControlStateNormal];
        self.isEdit = TableViewTypeIsEditing ;
    }

}
#pragma mark- 
#pragma mark- 请求数据

#pragma mark -
#pragma mark - 创建table
- (void)createTable
{
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(5, 0, self.view.frame.size.width - 10, self.view.frame.size.height - 49 - 64) style:UITableViewStylePlain];
    [self.table setBackgroundColor:[UIColor whiteColor]];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    
    // 隐藏滑动条
    self.table.showsVerticalScrollIndicator = NO;
    // 开始设置headertable
    
    // 设置tableview的footer
    self.table.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
    //静止来回晃动
    self.table.bounces = NO;
    [self.table release];
}

#pragma mark - 点击cell跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 跳到每本书的详情, 传值bookid
    BookDetailController *detail = [[BookDetailController alloc]init];
    detail.str = [NSString stringWithFormat:@"%@", [[self.tableArray objectAtIndex:indexPath.row] objectForKey:@"bookid"]];
    
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];
    
}

#warning 2:让哪一行可以被编辑 dataSource的方法

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    
        return YES;
}

#warning 3:设置cell的编辑样式delegate方法
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
        return UITableViewCellEditingStyleDelete;
    
}

#warning 4:提交编译结果
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        [CollectClass deleteArray:indexPath.row];
//        [self.tableArray removeObjectAtIndex:indexPath.row];
        [tableView reloadData];
    }

}


#pragma mark -
#pragma mark - required协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *array = [CollectClass readArray];
    self.tableArray = [NSMutableArray arrayWithArray:array];
    return self.tableArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndentity = @"cell";
    BookShelfCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentity];
    if (!cell) {
        cell = [[BookShelfCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIndentity];
    }
    cell.frame = CGRectMake(0, 0, self.view.frame.size.width, 80);
    // 点击不变色
    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
    // 在后面加大于号
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    
    
   
    [cell ViewWithData:[self.tableArray objectAtIndex:indexPath.row]];
    
    
    
        return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}
#pragma mark - 返回分区数
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    return 2;
}
#pragma mark - 返回标题数
#pragma mark - 标题







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
